package mumu.android.monitoring;

import com.nd.pad.sdk.usercenter.db.dao.KeyValueTableDao;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.telephony.TelephonyManager;
import android.widget.Toast;

public class NetCastReceiver extends BroadcastReceiver {

	static final String ACTION = "android.net.conn.CONNECTIVITY_CHANGE";

	@Override
	public void onReceive(Context context, Intent intent) {

		if (intent.getAction().equals(ACTION)) {
			KeyValueTableDao.getInstance(context).put(ACTION + "NetCastReceiver", System.currentTimeMillis() + "");
			mumu.android.lib.tool.MLog.d("dd", "NetCastReceiver");
			// TODO Auto-generated method stub
			// Toast.makeText(context, intent.getAction(), 1).show();
			ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
			NetworkInfo mobileInfo = manager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
			NetworkInfo wifiInfo = manager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
			NetworkInfo activeInfo = manager.getActiveNetworkInfo();
			if (activeInfo != null) {
				mumu.android.lib.tool.MLog.d("dd", "NetCastReceiver:" + activeInfo.getSubtype() + "-" + activeInfo.getSubtypeName());
			}
			if (activeInfo != null && wifiInfo != null && mobileInfo != null) {
				activeInfo.getSubtypeName();
				Toast.makeText(context, "mobile:" + mobileInfo.isConnected() + " " + "wifi:" + wifiInfo.isConnected() + " " + "active:" + activeInfo.getTypeName(), 1).show();
				mumu.android.lib.tool.MLog.d("dd", "mobile:" + mobileInfo.isConnected() + " " + "wifi:" + wifiInfo.isConnected() + " " + "active:" + activeInfo.getTypeName());
			}
		}
	}

	public boolean isNetworkConnected(Context context) {
		if (context != null) {
			ConnectivityManager mConnectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
			NetworkInfo mNetworkInfo = mConnectivityManager.getActiveNetworkInfo();
			if (mNetworkInfo != null) {
				return mNetworkInfo.isAvailable();
			}
		}
		return false;
	}

	public boolean isWifiConnected(Context context) {
		if (context != null) {
			ConnectivityManager mConnectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
			NetworkInfo mWiFiNetworkInfo = mConnectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
			if (mWiFiNetworkInfo != null) {
				return mWiFiNetworkInfo.isAvailable();
			}
		}
		return false;
	}

	public boolean isMobileConnected(Context context) {
		if (context != null) {
			ConnectivityManager mConnectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
			NetworkInfo mMobileNetworkInfo = mConnectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
			if (mMobileNetworkInfo != null) {
				return mMobileNetworkInfo.isAvailable();
			}
		}
		return false;
	}

	public static int getConnectedType(Context context) {
		if (context != null) {
			ConnectivityManager mConnectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
			NetworkInfo mNetworkInfo = mConnectivityManager.getActiveNetworkInfo();
			if (mNetworkInfo != null && mNetworkInfo.isAvailable()) {
				return mNetworkInfo.getType();
			}
		}
		return -1;
	}
}
